import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;


public class LogTest {
	String logFileName = "log/mlpd.log";
	String dataFileName = "log/data.txt";
	String logLine;
	TreeMap<String, ArrayList<Date>> dateMap = new TreeMap<String, ArrayList<Date>>();
	TreeMap<String, ArrayList<Location>> locMap = new TreeMap<String, ArrayList<Location>>();
	
	public static void main(String[] args) throws IOException, ParseException{
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		LogTest test = new LogTest();
		ArrayList<Date> dateList;
		ArrayList<Location> locationList;
		Entry<String, ArrayList<Date>> dateEntry;
		Entry<String, ArrayList<Location>> locationEntry;
		BufferedReader in = new BufferedReader(new FileReader(test.logFileName));
		
		while((test.logLine = in.readLine())!= null){
			if(test.logLine.length() > 59 && (test.logLine.charAt(46)=='[')){
				String phoneNumber = test.logLine.substring(47, 58);
				Date date = format.parse(test.logLine.substring(60, 79));
				int firstBracket = test.logLine.indexOf("(");
				int lastBracket = test.logLine.lastIndexOf(")");
				String location = test.logLine.substring(firstBracket, lastBracket);//提取出经纬度的位置
				int comma = location.indexOf(",");//经度和纬度之间的逗号
				double longitude = Double.parseDouble(location.substring(1, comma));//经度
				double latitude = Double.parseDouble(location.substring(comma+1, location.length()));//纬度
				LogTest.Location loc = test.new Location(longitude, latitude);
				
				if( (dateList = test.dateMap.get(phoneNumber)) == null ) {
					dateList = new ArrayList<Date>();
					dateList.add(date);
					test.dateMap.put(phoneNumber, dateList);
				} else {
					dateList.add(date);
				}
				if( (locationList = test.locMap.get(phoneNumber)) == null ) {
					locationList = new ArrayList<Location>();
					locationList.add(loc);
					test.locMap.put(phoneNumber, locationList);
				} else {
					locationList.add(loc);
				}
			}
		}
		in.close();
		//System.out.println(test.dateMap);

		StringBuilder sb = new StringBuilder();
		dateEntry = test.dateMap.pollFirstEntry();
		//System.out.println(dateEntry == null);
		locationEntry = test.locMap.pollFirstEntry();
		while(dateEntry != null && locationEntry != null) {
			String phoneNumber = dateEntry.getKey();
			sb.append(phoneNumber + " ");
			System.out.println(phoneNumber);

			dateList = dateEntry.getValue();
			locationList = locationEntry.getValue();
			long time = dateList.get(0).getTime();
			double la = locationList.get(0).latitude;
			double lo = locationList.get(0).longitude;
			for(int i = 1; i < dateList.size(); i++) {
				long time1 = dateList.get(i).getTime();
				double la1 = locationList.get(i).latitude;
				double lo1 = locationList.get(i).longitude;

				long timeDiff = (time1 - time) / 1000;
				double laDiff = la1 - la;
				double loDiff = lo1 - lo;

				sb.append(timeDiff + " ");
				sb.append(laDiff + " ");
				sb.append(loDiff + " ");
				sb.append("|" + " ");

				time = time1;
				la = la1;
				lo = lo1;
			}
			sb.append("\r\n");

			dateEntry = test.dateMap.pollFirstEntry();
			locationEntry = test.locMap.pollFirstEntry();
		}

		BufferedWriter out = new BufferedWriter(new FileWriter(test.dataFileName));
		PrintWriter pw = new PrintWriter(out);
		pw.print(sb.toString());
		out.close();
		
		System.out.println("Finish!");
	}


	class Location {
		public double latitude;
		public double longitude;
		public Location(double la, double lo) {
			latitude = la;
			longitude = lo;
		}
	}

}